package 栈;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: biubiu0002
 * @Date: 2021-09-12 14:42
 **/
public class 有效括号 {
    Map<Character,Character> map = new HashMap<>(){
        {
            put(')','(');
            put(']','[');
            put('}','{');
        }
    };
    public static void main(String[] args) {
        System.out.println(new 有效括号().isValid("[]"));


    }
    public boolean isValid(String s){

        Deque<Character> stack = new ArrayDeque<>();
        for(int i=0;i<s.length();i++){
            char c = s.charAt(i);
            if(c=='('||c=='['||c=='{'){
                stack.push(c);
            }else{
                if(!stack.isEmpty() && stack.peekLast().equals(map.get(c))){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        return true;

    }

}
